/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Comparator;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ResultsPager
/*     */ {
/*     */   private ZimbraQueryResults results;
/*     */   private boolean fixedOffset;
/*  43 */   private boolean ignoreOffsetHack = false;
/*     */   private List<ZimbraHit> bufferedHits;
/*     */   private SearchParams params;
/*  46 */   private boolean forward = true;
/*     */   private Comparator<ZimbraHit> comparator;
/*  48 */   private long offset = -1L;
/*     */   
/*     */   public static ResultsPager create(ZimbraQueryResults results, SearchParams params)
/*     */     throws ServiceException
/*     */   {
/*  53 */     params.setSortBy(results.getSortBy());
/*     */     
/*     */ 
/*  56 */     boolean dontUseCursor = false;
/*  57 */     boolean skipOffsetHack = false;
/*  58 */     switch (params.getSortBy()) {
/*     */     case TASK_DUE_ASC: 
/*     */     case TASK_DUE_DESC: 
/*     */     case TASK_PERCENT_COMPLETE_ASC: 
/*     */     case TASK_PERCENT_COMPLETE_DESC: 
/*     */     case TASK_STATUS_ASC: 
/*     */     case TASK_STATUS_DESC: 
/*  65 */       dontUseCursor = true;
/*  66 */       break;
/*     */     case NAME_LOCALIZED_ASC: 
/*     */     case NAME_LOCALIZED_DESC: 
/*  69 */       dontUseCursor = !DebugConfig.enableContactLocalizedSort;
/*     */       
/*     */ 
/*  72 */       skipOffsetHack = !DebugConfig.enableContactLocalizedSort;
/*     */     }
/*     */     
/*     */     
/*  76 */     if ((dontUseCursor) || (params.getCursor() == null)) {
/*  77 */       return new ResultsPager(results, params, false, skipOffsetHack);
/*     */     }
/*  79 */     return new ResultsPager(results, params, true, false);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private ResultsPager(ZimbraQueryResults results, SearchParams params, boolean useCursor, boolean skipOffset)
/*     */     throws ServiceException
/*     */   {
/*  88 */     this.results = results;
/*  89 */     this.params = params;
/*  90 */     this.fixedOffset = (!useCursor);
/*  91 */     this.ignoreOffsetHack = skipOffset;
/*     */     
/*  93 */     if (DebugConfig.enableContactLocalizedSort) {
/*  94 */       switch (params.getSortBy()) {
/*     */       case NAME_LOCALIZED_ASC: 
/*     */       case NAME_LOCALIZED_DESC: 
/*  97 */         this.comparator = params.getSortBy().getHitComparator(params.getLocale());
/*     */       }
/*     */       
/*     */     }
/* 101 */     reset();
/*     */   }
/*     */   
/*     */   public SortBy getSortOrder() {
/* 105 */     return this.params.getSortBy();
/*     */   }
/*     */   
/*     */   public void reset() throws ServiceException {
/* 109 */     if (this.fixedOffset) {
/* 110 */       int offsetToUse = this.params.getOffset();
/* 111 */       if (this.ignoreOffsetHack) {
/* 112 */         offsetToUse = 0;
/*     */       }
/* 114 */       if (offsetToUse > 0) {
/* 115 */         this.results.skipToHit(offsetToUse - 1);
/*     */       } else {
/* 117 */         this.results.resetIterator();
/*     */       }
/*     */     }
/* 120 */     else if (this.forward) {
/* 121 */       this.bufferedHits = new ArrayList(1);
/* 122 */       ZimbraHit current = forwardFindFirst();
/* 123 */       if (current != null)
/* 124 */         this.bufferedHits.add(current);
/*     */     } else {
/* 126 */       this.bufferedHits = backward();
/*     */     }
/*     */   }
/*     */   
/*     */   public long getCursorOffset()
/*     */   {
/* 132 */     return this.offset >= 0L ? this.offset + this.results.getCursorOffset() : -1L;
/*     */   }
/*     */   
/*     */   public boolean hasNext() throws ServiceException {
/* 136 */     if ((this.bufferedHits != null) && (!this.bufferedHits.isEmpty())) {
/* 137 */       return true;
/*     */     }
/* 139 */     return this.results.hasNext();
/*     */   }
/*     */   
/*     */   public ZimbraHit getNextHit() throws ServiceException
/*     */   {
/* 144 */     if ((this.bufferedHits != null) && (!this.bufferedHits.isEmpty())) {
/* 145 */       return (ZimbraHit)this.bufferedHits.remove(0);
/*     */     }
/* 147 */     return this.results.getNext();
/*     */   }
/*     */   
/*     */   private ZimbraHit forwardFindFirst() throws ServiceException
/*     */   {
/* 152 */     this.offset = -1L;
/* 153 */     ZimbraHit prevHit = getPrevCursorHit();
/* 154 */     this.results.resetIterator();
/* 155 */     ZimbraHit hit = this.results.getNext();
/* 156 */     while (hit != null) {
/* 157 */       this.offset += 1L;
/*     */       
/* 159 */       if (hit.getItemId() == this.params.getCursor().getItemId().getId()) {
/* 160 */         return this.results.getNext();
/*     */       }
/*     */       int comp;
/*     */       int comp;
/* 164 */       if ((DebugConfig.enableContactLocalizedSort) && (this.comparator != null)) {
/* 165 */         comp = this.comparator.compare(hit, prevHit);
/*     */       } else {
/* 167 */         comp = hit.compareTo(this.params.getSortBy(), prevHit);
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 173 */       if (comp == 0) {
/* 174 */         if (this.params.getCursor().getItemId().getId() == 0) {
/* 175 */           return hit;
/*     */         }
/* 177 */         if (this.params.getSortBy().getDirection() == SortBy.Direction.DESC) {
/* 178 */           if (hit.getItemId() < this.params.getCursor().getItemId().getId()) {
/* 179 */             return hit;
/*     */           }
/*     */         }
/* 182 */         else if (hit.getItemId() > this.params.getCursor().getItemId().getId()) {
/* 183 */           return hit;
/*     */         }
/*     */         
/*     */ 
/* 187 */         hit = this.results.getNext();
/* 188 */       } else if (comp < 0)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 195 */         hit = this.results.getNext();
/*     */       } else {
/* 197 */         return hit;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 202 */     this.offset = -1L;
/* 203 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */   private List<ZimbraHit> backward()
/*     */     throws ServiceException
/*     */   {
/* 210 */     List<ZimbraHit> result = new LinkedList();
/* 211 */     this.offset = -1L;
/* 212 */     this.results.resetIterator();
/* 213 */     ZimbraHit hit = this.results.getNext();
/* 214 */     ZimbraHit prevHit = getPrevCursorHit();
/*     */     
/* 216 */     ZimbraHit dummyEndHit = null;
/* 217 */     if (this.params.getCursor().getEndSortValue() != null) {
/* 218 */       dummyEndHit = getEndCursorHit();
/*     */     }
/*     */     
/* 221 */     while (hit != null) {
/* 222 */       this.offset += 1L;
/* 223 */       result.add(hit);
/* 224 */       if (hit.getItemId() == this.params.getCursor().getItemId().getId()) {
/*     */         break;
/*     */       }
/*     */       
/* 228 */       if (hit.compareTo(this.params.getSortBy(), prevHit) > 0) {
/*     */         break;
/*     */       }
/*     */       
/* 232 */       if ((this.params.getCursor().getEndSortValue() != null) && (hit.compareTo(this.params.getSortBy(), dummyEndHit) <= 0)) {
/*     */         break;
/*     */       }
/* 235 */       hit = this.results.getNext();
/*     */     }
/* 237 */     return result;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private ZimbraHit getPrevCursorHit()
/*     */   {
/* 244 */     return new CursorHit(this.results, this.params.getCursor().getSortValue(), this.params.getCursor().getItemId().getId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private ZimbraHit getEndCursorHit()
/*     */   {
/* 251 */     return new CursorHit(this.results, this.params.getCursor().getEndSortValue(), 0);
/*     */   }
/*     */   
/*     */   static final class CursorHit extends ZimbraHit {
/*     */     private final int idCursor;
/*     */     
/*     */     CursorHit(ZimbraQueryResults results, String sortValue, int id) {
/* 258 */       super(null, sortValue);
/* 259 */       this.idCursor = id;
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/* 264 */       return Objects.toStringHelper(this).add("id", this.idCursor).add("sortValue", this.sortValue).toString();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */     public int getConversationId()
/*     */     {
/* 272 */       return 0;
/*     */     }
/*     */     
/*     */     public int getItemId()
/*     */     {
/* 277 */       return this.idCursor;
/*     */     }
/*     */     
/*     */ 
/*     */     void setItem(MailItem item) {}
/*     */     
/*     */ 
/*     */     boolean itemIsLoaded()
/*     */     {
/* 286 */       return false;
/*     */     }
/*     */     
/*     */     public String getName()
/*     */     {
/* 291 */       return this.sortValue.toString();
/*     */     }
/*     */     
/*     */     public MailItem getMailItem()
/*     */     {
/* 296 */       return null;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ResultsPager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */